export default {
    data() {
        let validateNewPassword = (rule, value, callback) => {
            if (value === this.form.password) {
                callback(new Error('新密码不能与原密码相同!'))
            } else {
                callback()
            }
        }
        let validateNewPassword2 = (rule, value, callback) => {
            if (value !== this.form.newPassword) {
                callback(new Error('与新密码不一致!'))
            } else {
                callback()
            }
        }
        return {
            form: {},
            rules: {
                password: [
                    { required: true, message: '请输入原密码', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请设置新密码', trigger: 'blur' },
                    { validator: validateNewPassword, trigger: 'blur' }
                ],
                newPassword2: [
                    { required: true, message: '请确认新密码', trigger: 'blur' },
                    { validator: validateNewPassword2, trigger: 'blur' }
                ], pwd: [
                    { required: true, message: '请设置密码', trigger: 'blur' },
                    { pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,16}$/, message: '必须包含大小写字母、数字,且长度为6到16位', trigger: 'blur' }
                ],
                repwd: [
                    { required: true, message: '请确认密码', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (value === '') {
                                callback(new Error('请再次输入密码'))
                            } else if (value !== this.ruleForm.pwd) {
                                callback(new Error('两次输入密码不一致'))
                            } else {
                                callback()
                            }
                        },
                        trigger: 'blur'
                    }
                ],
            },
            showname: true,
            showpwd: true,
            user: {},
            sizeForm: {
                name: '',
                sex: '',
                password: '',
                phone: '',
                birth: '',
                imageUrl: '',
                address: '',
                newname: ''
            },
            myheaders: {
                Authorization: localStorage.getItem('token')
            }
        };
    },
    methods: {
        handleAvatarSuccess(res, file) {
            this.sizeForm.imageUrl = URL.createObjectURL(file.raw);
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        updatename() {
            this.showname = false
        },
        toupdatename() {
            let name = /^[a-zA-Z][a-zA-Z0-9]{3,14}$/
            if (!name.test(this.sizeForm.newname)) {
                this.$message({
                    message: '请填写符合条件的用户名',
                    type: 'error',
                    duration: 1000
                });
                return
            }
            this.user1 = JSON.parse(localStorage.getItem('currentUser'))
            this.axios.post('api/updUserName', { userId: this.user1._id, newname: this.sizeForm.newname })
                .then(res => {
                    console.log(' res111: ', res)
                })
            this.$message.success('用户名已修改')
            this.user1.user_name = this.sizeForm.newname
            this.sizeForm.name = this.sizeForm.newname
            this.showname = true
        },
        updatepwd() {
            this.showpwd = false
        },
        toupdatepwd(formName) {
            this.user2 = JSON.parse(localStorage.getItem('currentUser'))
            this.axios.post('/api/changePwd', { userId: this.user2._id, userOldPwd: this.form.password, userNewPwd: this.form.newPassword })
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.$message.success('密码已修改')
                } else {
                    this.$message.error('请正确填写表单')
                    return false
                }
            })
            this.showpwd = true
        }
    },
    created() {
        this.user = JSON.parse(localStorage.getItem('currentUser'))
        this.axios.defaults.headers.common['Authorization'] = localStorage.getItem('token')
        this.axios.get('api/getUserInfoById')
            .then(res => {
                this.user = res.data.data.find(data => {
                    return data._id == this.user._id
                })
                this.sizeForm.name = this.user.user_name
                this.sizeForm.phone = this.user.user_phone
                this.sizeForm.imageUrl = this.user.user_pic
                this.sizeForm.sex = this.user.user_sex ? '男' : '女'
                this.axios.get('api/getAddressInfo').then(res => {
                    this.address = res.data.data
                    console.log('address:', this.address)
                    if (this.address.length < 1) {
                        this.sizeForm.address = '待添加。。'
                    }
                    this.sizeForm.address = this.address[0].address_details
                })
            })
    }
}